﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Management.Automation;
using System.ComponentModel;
using System.Globalization;
using System.ComponentModel.Design.Serialization;
using System.Reflection;

namespace BGShell.PowerShell.Controls
{
    public sealed class ConsoleControlFormatSettings
    {
        private readonly Dictionary<PSTokenType, ConsoleControlFormatEntry> _entries =
            new Dictionary<PSTokenType, ConsoleControlFormatEntry>();

        private ConsoleControlFormatEntry _debug;
        private ConsoleControlFormatEntry _error;
        private ConsoleControlFormatEntry _verbose;
        private ConsoleControlFormatEntry _warning;

        private ConsoleControlFormatEntry _normal = new ConsoleControlFormatEntry(Color.LightGray, Color.Black, "Consolas", 9f);

        public ConsoleControlFormatSettings()
        {
            _debug = CloneNormal(Color.SpringGreen);
            _error = CloneNormal(Color.OrangeRed);
            _verbose = CloneNormal(Color.PeachPuff);
            _warning = CloneNormal(Color.Yellow);

            _entries[PSTokenType.Comment] = CloneNormal(Color.Green);
            _entries[PSTokenType.Comment].FontName = "Calibri";
            _entries[PSTokenType.Comment].FontSize = 10f;
            _entries[PSTokenType.Comment].Italic = true;
            
            _entries[PSTokenType.GroupStart] = CloneNormal(Color.White);
            _entries[PSTokenType.GroupEnd] = CloneNormal(Color.White);

            _entries[PSTokenType.Keyword] = CloneNormal(Color.DodgerBlue);
            _entries[PSTokenType.Keyword].Bold = true;

            _entries[PSTokenType.Operator] = CloneNormal(Color.White);
            
            _entries[PSTokenType.String] = CloneNormal(Color.PaleVioletRed);
            _entries[PSTokenType.Type] = CloneNormal(Color.LightSlateGray);
        }

        public ConsoleControlFormatEntry Debug
        {
            get { return _debug; }
            set { _debug = value; }
        }

        public ConsoleControlFormatEntry Error
        {
            get { return _error; }
            set { _error = value; }
        }

        public ConsoleControlFormatEntry Normal
        {
            get { return _normal; }
            set { _normal = value; }
        }

        public ConsoleControlFormatEntry Verbose
        {
            get { return _verbose; }
            set { _verbose = value; }
        }

        public ConsoleControlFormatEntry Warning
        {
            get { return _warning; }
            set { _warning = value; }
        }

        [CLSCompliant(false)]
        public ConsoleControlFormatEntry this[PSTokenType tokenType]
        {
            get
            {
                if (_entries.ContainsKey(tokenType))
                {
                    return _entries[tokenType];
                }

                return _normal;
            }
            set
            {
                if (Enum.IsDefined(typeof(PSTokenType), tokenType))
                {
                    _entries[tokenType] = value;
                }
            }
        }

        private ConsoleControlFormatEntry CloneNormal(Color foreColor)
        {
            var result = _normal.Clone();
            result.ForeColor = foreColor;

            return result;
        }
    }
}
